********Create lifetime experienced real GDP growth mean and real GDP growth standard deviation********

*Clean GDP growth data

cd "$reploc/data"
use Indo_prov_timeseries.dta, clear
keep provcode93 gdpgr*

*Winsorize province growth data at 5-95 level across all provinces

local levelwinsor=0.05

reshape long gdpgr, i(provcode93) j(year)
if `levelwinsor'>0 {
winsor gdpgr, p(`levelwinsor') gen(gdpgr_wins)
}
else {
gen gdpgr_wins=gdpgr
}
drop gdpgr
rename gdpgr_wins gdpgr
reshape wide gdpgr, i(provcode93) j(year)

tempfile prov_timeseries_wins
save "`prov_timeseries_wins'"

*Merge birth province data, and create location data
use "$reploc/data/created_data/indiv_prov_hist.dta", clear
keep pidlink 
tempfile indiv_timeseries
save "`indiv_timeseries'", replace

use "$reploc/data/created_data/indiv_prov_hist.dta", clear
egen provcode93 = rowmean(lcbir_*_93) 
merge m:1 provcode93 using "`prov_timeseries_wins'", gen(_merge1)
drop if _merge1 == 2	
forv i = 1977/2014{
rename gdpgr`i' grbr`i'
}
keep pidlink grbr*
merge 1:1 pidlink using "`indiv_timeseries'", gen(_merge2)
drop _merge2
save "`indiv_timeseries'", replace

*Merging in other variables in indiv_prov_hist that we care about
merge 1:1 pidlink using "$reploc/data/created_data/indiv_prov_hist.dta", gen(_merge3)
gen Prov2007=lc_2007_93
gen Prov2014=lc_2014_93
keep pidlink grbr* misprov_match nummisyr migrate* birprov_impl biryear_impl Prov2007 Prov2014 earliestyrmig*

*Merge in national timeseries
gen idind=1
merge m:1 idind using Indo_timeseries.dta, gen(_mergeindo)
drop idind indo_gdp*
drop _mergeindo


***Generate experienced mean and volatility of growth

gen imp_age2014 = 2014 - biryear_impl
gen imp_age2007 = 2007 - biryear_impl

local t grbr 

quietly {
local nums 07 14

order grbr*, sequential

*Variation 1: Assume missing for individuals born before 1977


local nums 07 14

local lam1=0
local lam2=0
local numi=5

	forval lambda1=`lam1'(10)`lam2' {
	
	
	capture drop *_wt107_lam* *_wt114_lam* sumweights* *lam*_07 *lam*_14 
	
		local lambda=round(`lambda1'/100,0.1)


		if `lambda'<0 {
			local hola=substr("`lambda1'",2,.)
			local lamnam neg`hola'
		}
		else {
			local lamnam `lambda1'
		}

			*Avg
		
		foreach wav in `nums' {
			foreach var of varlist `t'1977-`t'2014  {

				local curryear=substr("`var'",`numi',.)

	
				local k=20`wav'-`curryear'
				gen `var'_wt1`wav'_lam`lamnam'=.
			
				replace `var'_wt1`wav'_lam`lamnam'=((imp_age20`wav'-`k')/imp_age20`wav')^`lambda' if `k'<=imp_age20`wav' & `curryear'<=20`wav' & biryear_impl>=1977 & biryear_impl<=`curryear'
         
			
			   replace `var'_wt1`wav'_lam`lamnam'=((imp_age20`wav'-`k')/imp_age20`wav')^`lambda' if biryear_impl<=1976 & `curryear'<=20`wav'
				
			}
		


			egen sumweights`wav'_lam`lamnam'=rowtotal(*_wt1`wav'_lam`lamnam')


			foreach var of varlist `t'???? {

				local curryear=substr("`var'",`numi',.)
				gen `var'lam`lamnam'_`wav'=.
				replace `var'lam`lamnam'_`wav'=`var'*`var'_wt1`wav'_lam`lamnam'/sumweights`wav'_lam`lamnam' if biryear_impl<=`curryear' & biryear_impl>=1977
                replace `var'lam`lamnam'_`wav'=`var'*`var'_wt1`wav'_lam`lamnam'/sumweights`wav'_lam`lamnam' if biryear_impl<=`curryear' & biryear_impl<1977
				
			}
		}

		egen `t'provAvg_lam`lamnam'_7707=rowtotal(`t'????lam`lamnam'_07), missing
		egen `t'provAvg_lam`lamnam'_7714=rowtotal(`t'????lam`lamnam'_14), missing

	*SD

		foreach wav in `nums' {
			foreach var of varlist `t'???? {
			
				local curryear=substr("`var'",`numi',.)
				gen `var'lam`lamnam'sd_`wav'=.
				replace `var'lam`lamnam'sd_`wav'=`var'_wt1`wav'_lam`lamnam'*(`var'-`t'provAvg_lam`lamnam'_77`wav')^2 if biryear_impl<=`curryear' & biryear_impl>=1977
				replace `var'lam`lamnam'sd_`wav'=`var'_wt1`wav'_lam`lamnam'*(`var'-`t'provAvg_lam`lamnam'_77`wav')^2 if biryear_impl<=`curryear' & biryear_impl<1977
				
			}
		}
		
		capture drop intSD_lam*

		egen intSD_lam`lamnam'_7707=rowtotal(`t'????lam`lamnam'sd_07), missing
		egen intSD_lam`lamnam'_7714=rowtotal(`t'????lam`lamnam'sd_14), missing

		gen `t'provSD_lam`lamnam'_7707=sqrt((imp_age2007)*intSD_lam`lamnam'_7707/((imp_age2007-1)*sumweights07_lam`lamnam'))
		gen `t'provSD_lam`lamnam'_7714=sqrt((imp_age2014)*intSD_lam`lamnam'_7714/((imp_age2014-1)*sumweights14_lam`lamnam'))


		capture drop grbr????lam* intSD* *_wt1*_lam* sumweights*
		
	}
	
	
	capture drop grbr????lam*_?? intSD* *_wt1*_lam* sumweights*

	
*Variation 2: Attach Indonesian growth for people who are born before 1977 from 1961 to 1976

local nums 07 14

	local lam1=0
	local lam2=0
	local numi=5

		forval lambda1=`lam1'(10)`lam2' {
	
	
	capture drop *_wt107_lam* *_wt114_lam* sumweights* *lam*_07 *lam*_14 
	
		local lambda=round(`lambda1'/100,0.1)


		if `lambda'<0 {
			local hola=substr("`lambda1'",2,.)
			local lamnam neg`hola'
		}
		else {
			local lamnam `lambda1'
		}

*Avg
		
		foreach wav in `nums' {
		
		
		forval curryear=1961/2014 {
				
				local k=20`wav'-`curryear'
				gen v`curryear'_wt1`wav'_lam`lamnam'co2=.
			
				replace v`curryear'_wt1`wav'_lam`lamnam'co2=((imp_age20`wav'-`k')/imp_age20`wav')^`lambda' if `k'<=imp_age20`wav' & `curryear'<=20`wav' & biryear_impl>=1961 & biryear_impl<=`curryear'
				
		
			
				
			}
		


			egen sumweights`wav'_lam`lamnam'co2=rowtotal(*_wt1`wav'_lam`lamnam'co2)
			



				forval curryear=1961/2014 {

				
				gen v`curryear'lam`lamnam'_`wav'co2=.
				
				
				if `curryear'>=1977 {
				
				replace v`curryear'lam`lamnam'_`wav'co2=`t'`curryear'*v`curryear'_wt1`wav'_lam`lamnam'co2/sumweights`wav'_lam`lamnam'co2 if biryear_impl<=`curryear' & biryear_impl>=1961
				
				
				}
				
				if `curryear'<1977 {
				
								replace v`curryear'lam`lamnam'_`wav'co2=indo_gr`curryear'*v`curryear'_wt1`wav'_lam`lamnam'co2/sumweights`wav'_lam`lamnam'co2 if biryear_impl<=`curryear' & biryear_impl>=1961 & biryear_impl<=1976
				
				}
				
			}
		}

		egen `t'provAvg_lam`lamnam'_7707co2=rowtotal(v????lam`lamnam'_07co2), missing
		egen `t'provAvg_lam`lamnam'_7714co2=rowtotal(v????lam`lamnam'_14co2), missing

		*SD

		foreach wav in `nums' {
		forval curryear=1961/2014 {
				gen v`curryear'lam`lamnam'sd_`wav'co2=.
				
				
						if `curryear'>=1977 {
				
				replace v`curryear'lam`lamnam'sd_`wav'co2=v`curryear'_wt1`wav'_lam`lamnam'co2*(`t'`curryear'-`t'provAvg_lam`lamnam'_77`wav'co2)^2 if biryear_impl<=`curryear' & biryear_impl>=1961  


				}
				
					if `curryear'<1977 {
					
				replace v`curryear'lam`lamnam'sd_`wav'co2=v`curryear'_wt1`wav'_lam`lamnam'co2*(indo_gr`curryear'-`t'provAvg_lam`lamnam'_77`wav'co2)^2 if biryear_impl<=`curryear' & biryear_impl>=1961  & biryear_impl<=1976 
							
					}
				
			}
		}
		
		capture drop intSD_lam*

		egen intSD_lam`lamnam'_7707co2=rowtotal(v????lam`lamnam'sd_07co2), missing
		egen intSD_lam`lamnam'_7714co2=rowtotal(v????lam`lamnam'sd_14co2), missing


	
		gen `t'provSD_lam`lamnam'_7707co2=sqrt((imp_age2007)*intSD_lam`lamnam'_7707/((imp_age2007-1)*sumweights07_lam`lamnam'co2))
		gen `t'provSD_lam`lamnam'_7714co2=sqrt((imp_age2014)*intSD_lam`lamnam'_7714/((imp_age2014-1)*sumweights14_lam`lamnam'co2))

		
		capture drop v????lam* intSD* *_wt1*_lam* sumweights*


	}
	
capture drop v????lam* intSD* *_wt1*_lam* sumweights*
	
	

	

*Variation 3: Attach Indonesian	growth for everyone from ages 0-15

	local nums 07 14
	
		local lam1=0
	local lam2=0
	local numi=5
	
		forval lambda1=`lam1'(10)`lam2' {
	
	
	capture drop *_wt107_lam* *_wt114_lam* sumweights* *lam*_07 *lam*_14 
	
		local lambda=round(`lambda1'/100,0.1)

	

		if `lambda'<0 {
			local hola=substr("`lambda1'",2,.)
			local lamnam neg`hola'
		}
		else {
			local lamnam `lambda1'
		}


		*Avg
		foreach wav in `nums' {
		
		
		forval curryear=1961/2014 {
				
				local k=20`wav'-`curryear'
				gen v`curryear'_wt1`wav'_lam`lamnam'co3=.
			
				replace v`curryear'_wt1`wav'_lam`lamnam'co3=((imp_age20`wav'-`k')/imp_age20`wav')^`lambda' if `k'<=imp_age20`wav' & `curryear'<=20`wav' & biryear_impl>=1961 & biryear_impl<=`curryear'
				
		
			
				
			}
		


			egen sumweights`wav'_lam`lamnam'co3=rowtotal(*_wt1`wav'_lam`lamnam'co3)
			



				forval curryear=1961/2014 {

				
				gen v`curryear'lam`lamnam'_`wav'co3=.
				
				
				if `curryear'>=1977 {
				
				replace v`curryear'lam`lamnam'_`wav'co3=`t'`curryear'*v`curryear'_wt1`wav'_lam`lamnam'co3/sumweights`wav'_lam`lamnam'co3 if biryear_impl<=`curryear' & biryear_impl>=1961 & `curryear'-biryear_impl>15 
				
				replace v`curryear'lam`lamnam'_`wav'co3=indo_gr`curryear'*v`curryear'_wt1`wav'_lam`lamnam'co3/sumweights`wav'_lam`lamnam'co3 if biryear_impl<=`curryear' & biryear_impl>=1961 & `curryear'-biryear_impl<=15 
				
				}
				
				if `curryear'<1977 {
				
								replace v`curryear'lam`lamnam'_`wav'co3=indo_gr`curryear'*v`curryear'_wt1`wav'_lam`lamnam'co3/sumweights`wav'_lam`lamnam'co3 if biryear_impl<=`curryear' & biryear_impl>=1961 & `curryear'-biryear_impl<=15 
				
				}
				
			}
		}

		egen `t'provAvg_lam`lamnam'_7707co3=rowtotal(v????lam`lamnam'_07co3), missing
		egen `t'provAvg_lam`lamnam'_7714co3=rowtotal(v????lam`lamnam'_14co3), missing

*SD
		foreach wav in `nums' {
		forval curryear=1961/2014 {
				gen v`curryear'lam`lamnam'sd_`wav'co3=.
				
				
						if `curryear'>=1977 {
				
				replace v`curryear'lam`lamnam'sd_`wav'co3=v`curryear'_wt1`wav'_lam`lamnam'co3*(`t'`curryear'-`t'provAvg_lam`lamnam'_77`wav'co3)^2 if biryear_impl<=`curryear' & biryear_impl>=1961  & `curryear'-biryear_impl>15 

				replace v`curryear'lam`lamnam'sd_`wav'co3=v`curryear'_wt1`wav'_lam`lamnam'co3*(indo_gr`curryear'-`t'provAvg_lam`lamnam'_77`wav'co3)^2 if biryear_impl<=`curryear' & biryear_impl>=1961  & `curryear'-biryear_impl<=15 
				
				}
				
					if `curryear'<1977 {
					
									replace v`curryear'lam`lamnam'sd_`wav'co3=v`curryear'_wt1`wav'_lam`lamnam'co3*(indo_gr`curryear'-`t'provAvg_lam`lamnam'_77`wav'co3)^2 if biryear_impl<=`curryear' & biryear_impl>=1961  & `curryear'-biryear_impl<=15 
					
					}
				
			}
		}
		
		capture drop intSD_lam*

		egen intSD_lam`lamnam'_7707co3=rowtotal(v????lam`lamnam'sd_07co3), missing
		egen intSD_lam`lamnam'_7714co3=rowtotal(v????lam`lamnam'sd_14co3), missing


		gen `t'provSD_lam`lamnam'_7707co3=sqrt((imp_age2007)*intSD_lam`lamnam'_7707/((imp_age2007-1)*sumweights07_lam`lamnam'co3))
		gen `t'provSD_lam`lamnam'_7714co3=sqrt((imp_age2014)*intSD_lam`lamnam'_7714/((imp_age2014-1)*sumweights14_lam`lamnam'co3))

		capture drop v????lam* intSD* *_wt1*_lam* sumweights*

	}
	
	capture drop v????lam* intSD* *_wt1*_lam* sumweights*
	

	

	
}




keep pidlink grbr???? biryear_impl birprov_impl nummisyr misprov_match migrate07 migrate07_num migrate14 migrate14_num indo_gr???? *provAvg* *provSD* Prov2007 Prov2014 earliestyrmig*

*Drop duplicates
sort pidlink
quietly by pidlink: gen dup = cond(_N==1,0,_n)
drop if dup >1
drop dup



